

package com.maoshi.shop.security.comment.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoshi.shop.bean.event.LevelUpEvent;
import com.maoshi.shop.bean.model.User;
import com.maoshi.shop.bean.model.UserExtension;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.dao.UserExtensionMapper;
import com.maoshi.shop.dao.UserMapper;
import com.maoshi.shop.security.comment.dao.AppConnectMapper;
import com.maoshi.shop.security.comment.model.AppConnect;
import com.maoshi.shop.security.comment.service.AppConnectService;
import com.maoshi.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 *
 * @author lgh on 2018/09/07.
 */
@Service
@AllArgsConstructor
public class AppConnectServiceImpl extends ServiceImpl<AppConnectMapper, AppConnect> implements AppConnectService {

	private final AppConnectMapper appConnectMapper;
	private final ApplicationContext applicationContext;

	private final UserMapper userMapper;
	private final UserService userService;
	private final UserExtensionMapper userExtensionMapper;


	/**
	 * MaoshiUserServiceImpl#insertUserIfNecessary 将会清除该缓存信息
	 * @param bizUserId
	 * @return
	 */
	@Override
	public AppConnect getByBizUserId(String bizUserId, Integer appId) {
		return appConnectMapper.getByBizUserId(bizUserId, appId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public User registerOrBindUser(User user, AppConnect appConnect, Integer appId) {
		if (StrUtil.isBlank(user.getUserId())) {
			if (userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getUserName, user.getUserName())) > 0) {
				throw new MaoshiShopBindException("用户名已存在");
			}
			String userId = IdUtil.simpleUUID();
			user.setUserId(userId);
			user.setLevel(1);
			user.setLevelType(0);
			userMapper.insert(user);
			UserExtension userExtension = new UserExtension();
			userExtension.setBalance(0.0);
			userExtension.setGrowth(0);
			userExtension.setLevel(1);
			userExtension.setLevelType(0);
			userExtension.setScore(0);
			userExtension.setUpdateTime(new Date());
			userExtension.setVersion(0);
			userExtension.setUserId(userId);
			userExtensionMapper.insert(userExtension);
			//用户注册成功后发送等级提升事件
			applicationContext.publishEvent(new LevelUpEvent(userId,1,0));
		} else {
			if (appConnect != null&& StrUtil.isBlank(user.getPic())) {
				User userParam = new User();
				userParam.setUserId(user.getUserId());
				userParam.setModifyTime(new Date());
				userParam.setPic(appConnect.getImageUrl());
				userService.updateById(userParam);
			}
		}
		if (appConnect == null) {
			// 避免重复插入数据
			if (appConnectMapper.getByBizUserId(user.getUserId(), appId) != null) {
				return user;
			}
			appConnect = new AppConnect();
			appConnect.setUserId(user.getUserId());
			appConnect.setNickName(user.getNickName());
			appConnect.setImageUrl(user.getPic());

			// 0表示是系统的用户，不是第三方的
			appConnect.setAppId(appId);
			appConnectMapper.insert(appConnect);
		} else if (StrUtil.isBlank(appConnect.getUserId())) {
			appConnect.setUserId(user.getUserId());
			appConnectMapper.updateById(appConnect);
		}

		return user;
	}

	//合并代码开始
	@Override
	public AppConnect getByUnionId(String bizUnionId) {
		return appConnectMapper.getByUnionId(bizUnionId);
	}

	@Override
	public List<AppConnect> getAllAppConnect() {
		return appConnectMapper.getAllAppConnect();
	}

	@Override
	public void insert(AppConnect appConnect) {
		appConnectMapper.insert(appConnect);
	}

	@Override
	public AppConnect getByOpenId(String bizUserId) {
		return appConnectMapper.getByOpenId(bizUserId);
	}
	//合并代码结束

}
